var util = require('../../../utils/util.js');

Page({
  data: {
    shippingNo: '',
    shippingName: '',
    shippingCode: '',
    logistics: []
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      shippingNo: options.id,
      shippingCode: options.code,
      shippingName: options.name
    });
  },
  onShow: function () {
    var that = this;

    util.request('index/getTreaty', {
      type: 10
    }).then(businessId => {
      if (businessId.code === 0) {
        util.request('index/getTreaty', {
          type: 11
        }).then(appKey => {
          if (appKey.code === 0) {
            //使用插件调用物流轨迹
            var logisticsPlugin = requirePlugin("wt-logistics");
            let CustomerName = '';
            if (that.data.shippingCode === 'SF') {
              // 取手机号后四位
              CustomerName = that.mobile.substr(-4)
            }
            logisticsPlugin.reglogis({
              OrderCode: '',
              LogisticCode: that.data.shippingNo,
              ShipperCode: that.data.shippingCode,
              appId: businessId.data,
              appKey: appKey.data,
              requestType: '1002',
              dataType: '2',
              CustomerName: CustomerName,
              url: 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx'
            }).then(function (response) {
              if (response.Success) {
                that.setData({
                  logistics: response.Traces
                });
              } else {
                util.showMsg(response.Reason)
              }
            })
          }
        });
      }
    });
  }
})
